package com.zzp.fastdfs.file.service.impl;

import com.zzp.fastdfs.file.service.FastDFSService;
import com.zzp.fastdfs.file.utils.UserAgentUtils;
import com.zzp.fastdfs.file.wrapper.FastDFSWrapper;
import eu.bitwalker.useragentutils.Browser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * @author 佐斯特勒
 * <p>
 *  fastDFS服务的实现类
 * </p>
 * @version v1.0.0
 * @date 2020/1/28 1:38
 * @see  FastDFSServiceImpl
 **/
@Service
@Slf4j
public class FastDFSServiceImpl implements FastDFSService {
    @Resource
    private FastDFSWrapper fastDFSWrapper;

    @Override
    public String uploadFile(MultipartFile file,HttpServletRequest req) {
        printLog(req);
        var bytes = new byte[0];
        try {
            bytes = file.getBytes();
        } catch (IOException e) {
            log.error("获取文件错误");
            e.printStackTrace();
        }
        //获取源文件名称
        var originalFileName = file.getOriginalFilename();
        //获取文件后缀--.doc
        assert originalFileName != null;
        var extension = originalFileName.substring(originalFileName.lastIndexOf(".") + 1);
        var fileName = file.getName();
        //获取文件大小
        var fileSize = file.getSize();
        log.info(originalFileName + "==" + fileName + "==" + fileSize + "==" + extension + "==" + bytes.length);
        return fastDFSWrapper.uploadFile(bytes, fileSize, extension);
    }

    @Override
    public String[] uploadThumbImage(MultipartFile imgFile,HttpServletRequest req) {
        printLog(req);
        var bytes = new byte[0];
        try {
            bytes = imgFile.getBytes();
        } catch (IOException e) {
            log.error("图片上传异常");
            e.printStackTrace();
        }
        var originalFilename = imgFile.getOriginalFilename();
        assert originalFilename != null;
        var extension = originalFilename.substring(originalFilename.indexOf(".") + 1);
        var fileName = imgFile.getName();
        var size = imgFile.getSize();
        log.info(originalFilename + "==" + fileName + "==" + size + "==" + extension + "==" + bytes.length);
        return fastDFSWrapper.uploadThumbFile(bytes, size, extension);
    }

    @Override
    public void downloadFile(String fileUrl, HttpServletResponse response) throws IOException {
        var bytes = fastDFSWrapper.downloadFile(fileUrl);
        // 这里只是为了整合fastdfs，所以写死了文件格式。需要在上传的时候保存文件名。下载的时候使用对应的格式
        response.setHeader("Content-disposition", "attachment;filename="
                + URLEncoder.encode(fileUrl.substring(fileUrl.lastIndexOf("/") + 1), StandardCharsets.UTF_8));
        response.setCharacterEncoding("UTF-8");
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            outputStream.write(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                assert outputStream != null;
                outputStream.flush();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void delFile(String fileUrl ,HttpServletRequest req) {
        printLog(req);
        fastDFSWrapper.delFile(fileUrl);
    }

    /**
     * 打印请求信息
     * @param req .
     */
    private void printLog(HttpServletRequest req){
        var browser = UserAgentUtils.getBrowser(req);
        var ipAddr = UserAgentUtils.getIpAddr(req);
        log.info("浏览器:"+browser.getName()+"---"+"ip:"+ipAddr);
    }
}
